package com.xxc.hs.funActivity.panel;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

public class QuestionDao {
    private EelFaqDatabase dbHelper;

    public QuestionDao(Context context) {
        dbHelper = new EelFaqDatabase(context);
    }

    // 获取所有问题
    public List<Question> getAllQuestions() {
        List<Question> questionList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        String[] projection = {
                EelFaqDatabase.COLUMN_QUESTION_ID,
                EelFaqDatabase.COLUMN_QUESTION_TITLE,
                EelFaqDatabase.COLUMN_QUESTION_DESCRIPTION,
                EelFaqDatabase.COLUMN_QUESTION_EXPERT_ID,
                EelFaqDatabase.COLUMN_QUESTION_IMAGE_ID // 包含图片列
        };

        Cursor cursor = db.query(
                EelFaqDatabase.TABLE_QUESTIONS,
                projection,
                null,
                null,
                null,
                null,
                EelFaqDatabase.COLUMN_QUESTION_ID + " ASC"
        );

        if (cursor != null && cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_ID));
                String title = cursor.getString(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_TITLE));
                String description = cursor.getString(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_DESCRIPTION));
                int expertId = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_EXPERT_ID));
                int imageResId = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_IMAGE_ID));

                questionList.add(new Question(id, title, description, expertId, imageResId));
            } while (cursor.moveToNext());
            cursor.close();
        }

        db.close();
        return questionList;
    }

    // 搜索问题
    public List<Question> searchQuestions(String query) {
        List<Question> questionList = new ArrayList<>();
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        String[] projection = {
                EelFaqDatabase.COLUMN_QUESTION_ID,
                EelFaqDatabase.COLUMN_QUESTION_TITLE,
                EelFaqDatabase.COLUMN_QUESTION_DESCRIPTION,
                EelFaqDatabase.COLUMN_QUESTION_EXPERT_ID,
                EelFaqDatabase.COLUMN_QUESTION_IMAGE_ID
        };

        String selection = EelFaqDatabase.COLUMN_QUESTION_TITLE + " LIKE ? OR " +
                EelFaqDatabase.COLUMN_QUESTION_DESCRIPTION + " LIKE ?";
        String[] selectionArgs = {"%" + query + "%", "%" + query + "%"};

        Cursor cursor = db.query(
                EelFaqDatabase.TABLE_QUESTIONS,
                projection,
                selection,
                selectionArgs,
                null,
                null,
                EelFaqDatabase.COLUMN_QUESTION_ID + " ASC"
        );

        if (cursor != null && cursor.moveToFirst()) {
            do {
                int id = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_ID));
                String title = cursor.getString(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_TITLE));
                String description = cursor.getString(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_DESCRIPTION));
                int expertId = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_EXPERT_ID));
                int imageResId = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_IMAGE_ID));

                questionList.add(new Question(id, title, description, expertId, imageResId));
            } while (cursor.moveToNext());
            cursor.close();
        }

        db.close();
        return questionList;
    }

    // 根据ID获取问题
    public Question getQuestionById(int id) {
        Question question = null;
        SQLiteDatabase db = dbHelper.getReadableDatabase();

        String[] projection = {
                EelFaqDatabase.COLUMN_QUESTION_ID,
                EelFaqDatabase.COLUMN_QUESTION_TITLE,
                EelFaqDatabase.COLUMN_QUESTION_DESCRIPTION,
                EelFaqDatabase.COLUMN_QUESTION_EXPERT_ID,
                EelFaqDatabase.COLUMN_QUESTION_IMAGE_ID
        };

        String selection = EelFaqDatabase.COLUMN_QUESTION_ID + " = ?";
        String[] selectionArgs = {String.valueOf(id)};

        Cursor cursor = db.query(
                EelFaqDatabase.TABLE_QUESTIONS,
                projection,
                selection,
                selectionArgs,
                null,
                null,
                null
        );

        if (cursor != null && cursor.moveToFirst()) {
            int questionId = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_ID));
            String title = cursor.getString(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_TITLE));
            String description = cursor.getString(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_DESCRIPTION));
            int expertId = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_EXPERT_ID));
            int imageResId = cursor.getInt(cursor.getColumnIndexOrThrow(EelFaqDatabase.COLUMN_QUESTION_IMAGE_ID));

            question = new Question(questionId, title, description, expertId, imageResId);
            cursor.close();
        }

        db.close();
        return question;
    }
}